package daizuo;

import daizuo.utils.Readerinfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author cyt
 * @create 2020-07-21 12:01
 */
public class Menu {

    public static void selectUserMenu() throws IOException {
        int u1 = 1;
        int u2 = 2;
        int u3 = 3;
        int u4 = 4;
        System.out.println("请选择，输入数字： (非菜单数字则返回上一级)");

        int select = new Scanner(System.in).nextInt();
        if (select == u1) {
            UserMenu.UserMenu();
        } else if (select == u2) {
            GoodMenu.GoodList();
        } else if (select == u3) {
            Discount.DiscountMenu();
        } else if (select == u4) {
            exitSys();
        } else {
            printfMenu();
        }
    }

    public static void mainMenu() throws IOException {
        System.out.println("------------------");
        System.out.println("1、客户信息管理");
        System.out.println("2、购物结算");
        System.out.println("3、真情回馈");
        System.out.println("4、注销");
        System.out.println("------------------");
        selectMenu();
    }

    public static void exitSys() {
        System.exit(0);
    }

    public static void changePassword() throws IOException {
        System.out.println("密码重置成功");
        printfMenu();
    }

    public static void login() throws IOException {
        System.out.println("请输入用户名： ");
        String username = new Scanner(System.in).next();
        System.out.println("请输入密码： ");
        String password = new Scanner(System.in).next();
        ArrayList<String> user = Readerinfo.read("user");
        boolean flag = false;
        for (String str : user) {
            String s = str;
            String[] split = s.split(",");
            if (username.equals(password) && password.equals(split[1])) {
flag = true;
            } else {
              flag = false;
            }
        }

        if(flag) {
            mainMenu();
            selectUserMenu();
        }else{
            printfMenu();
        }
    }

    /**
     * 主菜单
     *
     * @throws IOException
     */
    public static void selectMenu() throws IOException {
        int a1 = 1;
        int a2 = 2;
        int a3 = 3;
        int a4 = 4;
        System.out.println("请选择，输入数字： ");
        int select = new Scanner(System.in).nextInt();
        if (select == a1) {
            login();
        } else if (select == a2) {
            GoodMenu.GoodList();
        } else if (select == a3) {
            Discount.DiscountMenu();
        } else if (select == a4) {
            System.exit(0);
        }
    }

    public static void printfMenu() throws IOException {
        System.out.println("------------------");
        System.out.println("1、登录系统");
        System.out.println("2、更改密码");
        System.out.println("3、退出");
        System.out.println("------------------");
        selectMenu();
    }

    public static void main(String[] args) throws IOException {
        printfMenu();
    }
}
